package com.maya.medicalwastes;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextWatcher;
import android.widget.EditText;
import android.widget.TextView;

import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;

import com.maya.medicalwastes.bean.QueryInfoBean;
import com.maya.medicalwastes.entity.QueryNurseInfoBean;
import com.maya.medicalwastes.httpNet.ApiService;
import com.maya.medicalwastes.httpNet.HttpService;
import com.maya.medicalwastes.httpNet.callback.ResponseCallBack;
import com.maya.medicalwastes.httpNet.common.SharedPreferencesConstant;
import com.maya.medicalwastes.httpNet.manage.RetrofitManager;
import com.maya.medicalwastes.httpNet.model.ResultModel;
import com.maya.medicalwastes.ui.WarehouseActivity;
import com.maya.medicalwastes.util.MyUtils;
import com.maya.medicalwastes.util.PrefUtils;
import com.maya.medicalwastes.util.StringUtils;
import com.maya.medicalwastes.util.ToastUtils;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import io.reactivex.Observable;

/**
 * @author created by fuguoyong
 * 版本：1.0
 * 创建日期：2020-11-19 15
 * 描述：DoctorActivity
 */
public class WareHouseActivity extends AppCompatActivity {

    @BindView(R.id.edt_scan)
    EditText mEditText;
     @BindView(R.id.titel)
     TextView titel;
    private String type="";
    @BindView(R.id.tv_title)
    TextView tv_title;
    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_doctor);
        ButterKnife.bind(this);

        Bundle bundle = getIntent().getExtras();
        if (bundle != null) {
            type = bundle.getString("type", "");
        }
        if (type.equals("1")){
            titel.setText("请扫描暂存点仓库识别码");
            tv_title.setText("请扫描暂存点仓库识别码");
        }else {
            titel.setText("请扫描仓库二维码");
            tv_title.setText("请扫描暂存点仓库识别码");
        }
        mEditText.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence charSequence, int i, int i1, int i2) {
            }

            @Override
            public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {

            }
            @Override
            public void afterTextChanged(Editable editable) {
                if (editable.toString().trim().contains("=") ) {
                    if (editable.toString().trim().contains("CK")){
                        wareHouse(editable.toString());
                    }else {
                        mEditText.setText("");
                    }
                }
            }
        });
    }
    public void wareHouse(String code) {
        HashMap<String, Object> params;
        params = new HashMap<>();
        params.put("codeInformation", code);
        params.put(ParamConstant.TOKEN, PrefUtils.getString(this, SharedPreferencesConstant.TOKEN, ""));
        Observable<ResultModel<QueryInfoBean>> call = RetrofitManager.getInstance().getRetrofit().create(ApiService.class).queryQrcodeInfo(params);
        HttpService.getInstance().enqueue(call, new ResponseCallBack<ResultModel<QueryInfoBean>>() {
            @Override
            public void onResponse(ResultModel<QueryInfoBean> response) {
                //   closeDialog();
                if (response != null && response.httpCode == 200) {
                    Bundle bundle = new Bundle();
                    if (response.data.getOrganId()!=null){
                        bundle.putString("ID", response.data.getId());
                        bundle.putString("warehouse", response.data.getWarehouseName());
                    }
                    if (type.equals("1")){
                        MyUtils.IntentToOther(WareHouseActivity.this, WarehouseActivity.class, bundle);

                    }else {
                        MyUtils.IntentToOther(WareHouseActivity.this, ForWarderActivity.class, bundle);

                    }
                    finish();
                }
            }

            @Override
            public void onFailure(int httpCode) {
                mEditText.setText("");
                //   closeDialog();
            }
        }, this);
    }

}
